package com.cms.plugin.hook.listener;

import com.cms.common.hook.enums.PluginsTypeEnum;
import com.cms.common.model.entity.Plugin;
import com.cms.plugin.hook.HookServices;
import com.cms.html.TemplateEngineService;
import com.cms.plugin.services.PluginService;
import com.gitee.starblues.core.PluginInfo;
import com.gitee.starblues.integration.listener.PluginListener;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.File;
import java.nio.file.Path;

@Component
@Slf4j
public class HookPluginListener implements PluginListener {


    @Autowired
    PluginService pluginService;

    //Plugin load success
    @Override
    public void loadSuccess(PluginInfo pluginInfo) {
        log.info("Plugin [{}] loaded successfully.", pluginInfo.getPluginId());
    }

    //Plugin load failure
    @Override
    public void loadFailure(Path path, Throwable throwable) {
        log.info("Failed to load plugin [{}]. {}", path, throwable.getMessage());
    }

    //Plugin unload success
    @Override
    public void unLoadSuccess(PluginInfo pluginInfo) {
        log.info("Plugin [{}] unloaded successfully.", pluginInfo.getPluginId());

    }

    //Plugin unload failure
    @Override
    public void unLoadFailure(PluginInfo pluginInfo, Throwable throwable) {
        log.info("Failed to unload plugin [{}]. {}", pluginInfo.getPluginId(), throwable.getMessage());
    }

    //Plugin start success
    @Override
    public void startSuccess(PluginInfo pluginInfo) {
        log.info("Plugin [{}] started successfully.", pluginInfo.getPluginId());

        initPlugin(pluginInfo);
        HookServices.initHookMap(pluginInfo);
        TemplateEngineService.initTemplatePath();
        if (!TemplateEngineService.templatesPath.containsKey(pluginInfo.getPluginId())) {
            String path = pluginInfo.getPluginPath().replace("\\","/");
            String[] tmpPaths = path.split("/");
            String layoutProject = tmpPaths[tmpPaths.length - 1];
            TemplateEngineService.templatesPath.put(layoutProject, path + "/target/classes/templates");
        }

    }

    //Plugin start failure
    @Override
    public void startFailure(PluginInfo pluginInfo, Throwable throwable) {
        log.info("Failed to start plugin [{}]. {}", pluginInfo.getPluginId(), throwable.getMessage());
    }

    //Plugin stop success
    @Override
    public void stopSuccess(PluginInfo pluginInfo) {
        log.info("Plugin [{}] stopped successfully.", pluginInfo.getPluginId());
        HookServices.delHookMap(pluginInfo);
        //permissionService.delPluginPermission(pluginInfo);
    }

    //Plugin stop failure
    @Override
    public void stopFailure(PluginInfo pluginInfo, Throwable throwable) {
        log.info("Failed to stop plugin [{}]. {}", pluginInfo.getPluginId(), throwable.getMessage());
    }

    //Initialize plugin
    private void initPlugin(PluginInfo pluginInfo) {
        Plugin plugins = pluginService.findByPluginId(pluginInfo.getPluginId());
        if (plugins == null) {
            plugins = new Plugin();
            plugins.setName(pluginInfo.getPluginDescriptor().getDescription());
            plugins.setPluginId(pluginInfo.getPluginId());
            plugins.setDescription(pluginInfo.getPluginDescriptor().getDescription());
            String pluginPath = pluginInfo.getPluginDescriptor().getPluginPath();
            PluginsTypeEnum[] typeEnums = PluginsTypeEnum.values();
            for (PluginsTypeEnum pluginsTypeEnum : typeEnums) {
                if (pluginPath.indexOf(File.separatorChar + pluginsTypeEnum.getPluginDir() + File.separatorChar) > -1) {
                    plugins.setType(pluginsTypeEnum.getPluginDir());
                    break;
                }
            }
            pluginService.init(plugins);
        }
    }
}
